﻿using DevelopTool.Bll.DesktopClockBlls;
using DevelopTool.Common;
using DevelopTool.Forms.BaseForms;
using Sunny.UI;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;

namespace DevelopTool.Forms.ToolForms.DesktopClock
{
    /// <summary>
    /// 桌面时钟
    /// </summary>
    public partial class DesktopClockForm : BaseUIForm
    {
        public DesktopClockForm()
        {
            InitializeComponent();
        }

        private FontDialog fontDialog = new FontDialog();


        private void ScreenShotForm_Load(object sender, EventArgs e)
        {
            this.chbShowDate.Checked = Setting.DesktopClock.ShowDate;
            this.barFontSize.Value = Setting.DesktopClock.FontSize.ToInt();
            this.lblFontSize.Text = Setting.DesktopClock.FontSize.ToString();

            List<Screen> screens = Screen.AllScreens.OrderBy(it => it.DeviceName).ToList();
            for (int i = 0; i < screens.Count; i++)
            {
                UICheckBox checkBox = new UICheckBox();
                checkBox.SetDPIScale();
                checkBox.Text = $"显示器[{i + 1}]";
                checkBox.Tag = screens[i];
                checkBox.TagString = screens[i].DeviceName;
                checkBox.CheckedChanged += CheckBox_CheckedChanged;
                flpMonitors.Add(checkBox);

                if (GetAllClockForm().Exists(it => it.ScreenId == screens[i].DeviceName))
                {
                    checkBox.Checked = true;
                }
            }

            ThemHelper.SetSystemThem(this);
        }


        private void CheckBox_CheckedChanged(object sender, EventArgs e)
        {
            if (sender is UICheckBox checkBox)
            {

                if (checkBox.Checked)
                {
                    if (GetAllClockForm().Exists(it => it.ScreenId == checkBox.TagString)) return;

                    ClockForm clockForm = new ClockForm((Screen)checkBox.Tag)
                    {
                        ScreenId = checkBox.TagString
                    };
                    clockForm.Show();
                }
                else
                {
                    ClockForm clockForm = GetAllClockForm().FirstOrDefault(it => it.ScreenId == checkBox.TagString);
                    if (clockForm != null)
                    {
                        clockForm.Close();
                    }

                }

                Setting.DesktopClock.Screens = GetAllClockForm().Select(it => it.ScreenId).ToList();
            }
        }


        private void btnChooseFont_Click(object sender, EventArgs e)
        {
            fontDialog.Font = new System.Drawing.Font(Setting.DesktopClock.FontFamily, Setting.DesktopClock.FontSize, (FontStyle)Enum.Parse(typeof(FontStyle), Setting.DesktopClock.FontStyle, false)); ;
            fontDialog.AllowScriptChange = false;
            fontDialog.ShowColor = false;
            fontDialog.ShowApply = true;
            fontDialog.ShowEffects = false;
            fontDialog.AllowSimulations = false;
            fontDialog.Apply += FontDialog_Apply;
            if (fontDialog.ShowDialog() == DialogResult.OK)
            {
                foreach (ClockForm clockForm in this.GetAllClockForm())
                {
                    clockForm.LblTime.Font = fontDialog.Font;
                }

                Setting.DesktopClock.FontFamily = fontDialog.Font.FontFamily.Name;
                Setting.DesktopClock.FontSize = fontDialog.Font.Size;
                Setting.DesktopClock.FontStyle = fontDialog.Font.Style.ToString();
            }
        }

        private void FontDialog_Apply(object sender, EventArgs e)
        {
            foreach (ClockForm clockForm in this.GetAllClockForm())
            {
                clockForm.LblTime.Font = fontDialog.Font;
            }

            Setting.DesktopClock.FontFamily = fontDialog.Font.FontFamily.Name;
            Setting.DesktopClock.FontSize = fontDialog.Font.Size;
            Setting.DesktopClock.FontStyle = fontDialog.Font.Style.ToString();
        }

        private void btnChooseColor_Click(object sender, EventArgs e)
        {
            ColorDialog colorDialog = new ColorDialog();
            colorDialog.AllowFullOpen = true;
            colorDialog.FullOpen = true;
            colorDialog.ShowHelp = false;
            colorDialog.Color = Setting.DesktopClock.ForeColor;
            colorDialog.AnyColor = true;

            if (colorDialog.ShowDialog() == DialogResult.OK)
            {
                foreach (ClockForm clockForm in this.GetAllClockForm())
                {
                    clockForm.LblTime.RectColor = colorDialog.Color;
                }

                Setting.DesktopClock.ForeColor = colorDialog.Color;
            }
        }

        private void barFontSize_ValueChanged(object sender, EventArgs e)
        {
            this.lblFontSize.Text = this.barFontSize.Value.ToString();
            foreach (ClockForm clockForm in this.GetAllClockForm())
            {
                clockForm.LblTime.Font = new System.Drawing.Font(clockForm.LblTime.Font.FontFamily, this.barFontSize.Value, clockForm.LblTime.Font.Style);
            }

            Setting.DesktopClock.FontSize = this.barFontSize.Value;
        }

        private void chbShowDate_CheckedChanged(object sender, EventArgs e)
        {
            Setting.DesktopClock.ShowDate = this.chbShowDate.Checked;
        }

        private List<ClockForm> GetAllClockForm()
        {
            return System.Windows.Forms.Application.OpenForms.Cast<Form>().Where(it => it.GetType() == typeof(ClockForm)).Select(it =>
            {
                return (ClockForm)it;
            }).ToList();
        }
    }
}
